package com.doctor.adapter;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.security.auth.Destroyable;

import com.doctor.ui.R;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class MyPagerAdapter extends PagerAdapter{

	/**
	 * 用于装日历控件的ViewPager
	 */
	
	private static final int initiaPosition=10002;
	private static final int pageCount=5;        //viewPager包含5个滑动页
	private Context context;
	private Calendar calendar;
	private Calendar nowCalendar;
	private boolean isInitiaFinished=false;
	private Map<Integer, List<Object>> map;
	private int currPosition;           //当前显示第几屏
	private MyPagerAdapterCallback callback;
	
	public MyPagerAdapter(Context context,MyPagerAdapterCallback callback){
		this.context=context;
		calendar=Calendar.getInstance();
		nowCalendar=Calendar.getInstance();
		map=new HashMap<Integer,List<Object>>();
		this.callback=callback;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return Integer.MAX_VALUE;
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		// TODO Auto-generated method stub
		return arg0==arg1;
	}
	
	@Override
	public void destroyItem(View container, int position, Object object) {
		// TODO Auto-generated method stub
		
		int transformedPosition=position%pageCount;
		GridView gridView=(GridView) map.get(transformedPosition).get(0);
		((ViewPager)container).removeView(gridView);
		
	}
	
	@Override
	public Object instantiateItem(View container, int position) {
		// TODO Auto-generated method stub
		//无限循环滑动
		int transformedPosition=position%pageCount;
		
		if (isInitiaFinished) {
			if (position>currPosition) {
				//向右滑动
				calendar.add(Calendar.MONTH, 2);
				setCalendar(calendar, transformedPosition, map);
				currPosition+=1;
				calendar.add(Calendar.MONTH, -1);
				callback.onSetYearAndMonth(calendar.get(Calendar.YEAR)+"年"+(calendar.get(Calendar.MONTH)+1)+"月");
				
			} else if (position<currPosition) {
				//向左滑动
				calendar.add(Calendar.MONTH, -2);
				setCalendar(calendar, transformedPosition, map);
				currPosition-=1;
				calendar.add(Calendar.MONTH, 1);
				callback.onSetYearAndMonth(calendar.get(Calendar.YEAR)+"年"+(calendar.get(Calendar.MONTH)+1)+"月");
			}			
		} else {
			currPosition=initiaPosition;
			switch (position) {
			case initiaPosition:
				setCalendar(calendar, transformedPosition, map);
				callback.onSetYearAndMonth(calendar.get(Calendar.YEAR)+"年"+(calendar.get(Calendar.MONTH)+1)+"月");
				break;

			case initiaPosition-1:
				calendar.add(Calendar.MONTH, -1);
				setCalendar(calendar, transformedPosition, map);
				break;
				
			case initiaPosition+1:
				calendar.add(Calendar.MONTH, 2);
			    setCalendar(calendar, transformedPosition, map);
			    calendar.add(Calendar.MONTH, -1);
			    break;
			}
		}
		
		if (position==initiaPosition+1) {
			//初始化3个页面完成
			isInitiaFinished=true;
		}
		GridView gridView=(GridView) map.get(transformedPosition).get(0);
		
		((ViewPager)container).addView(gridView ,0);		
		return gridView;
	}
	
	/**
	 * 设置日历
	 * @param calendar
	 * @param transformedPosition 转换后要显示的循环position
	 * @param map 存储gridView和adapter
	 */
	private void setCalendar(Calendar calendar,int transformedPosition,Map<Integer, List<Object>> map){
		int firstDay=calendar.getActualMinimum(calendar.DAY_OF_MONTH);
		int lastDay=calendar.getActualMaximum(calendar.DAY_OF_MONTH);
		int currDay=calendar.get(Calendar.DAY_OF_MONTH);
		calendar.add(Calendar.DAY_OF_MONTH, firstDay-currDay);
		//月初是周几
		int week=calendar.get(Calendar.DAY_OF_WEEK)-1;
		
		int itemCount;
		List<String> list=new ArrayList<String>();
		if (week==7) {
			itemCount=lastDay;
		} else {
			itemCount=lastDay+week;
			for (int i = 1; i <= week; i++) {
				list.add("");
			}
		}
		for (int i = 1; i <= lastDay; i++) {
			list.add(i+"");
		}
		int lines=(itemCount%7==0)?itemCount/7:(itemCount/7)+1;
		int totalCount=lines*7;
		
		if (totalCount-itemCount>0) {
			for (int i = itemCount+1; i <= totalCount; i++) {
				list.add("");
			}
		}
		
		//判断是否本年本月
		boolean isNow=false;
		int year=calendar.get(Calendar.YEAR);
		int month=calendar.get(Calendar.MONTH);
		int nowYear=nowCalendar.get(Calendar.YEAR);
		int nowMonth=nowCalendar.get(Calendar.MONTH);
		if (year==nowYear && month==nowMonth) {
			isNow=true;
		}
		
		if (map!=null && map.containsKey(transformedPosition)) {
			List<Object> gridViewAndAdapter=map.get(transformedPosition);
			GridView gridView=(GridView) gridViewAndAdapter.get(0);
			CalendarAdapter gridViewAdapter=(CalendarAdapter) gridViewAndAdapter.get(1);
			gridViewAdapter=null;
			
			gridViewAdapter=new CalendarAdapter(context,gridView,calendar,list,lines,isNow);
			gridView.setAdapter(gridViewAdapter);			
			//更新gridView和adapter后把新value重新put到map里
			gridViewAndAdapter.clear();
			gridViewAndAdapter.add(gridView);
			gridViewAndAdapter.add(gridViewAdapter);
			map.put(transformedPosition, gridViewAndAdapter);
		} else {
			AbsListView.LayoutParams params=new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,
					AbsListView.LayoutParams.MATCH_PARENT);
			GridView gridView=new GridView(context);
			gridView.setLayoutParams(params);
			gridView.setBackgroundResource(R.color.gray_line);
			gridView.setNumColumns(7);
			gridView.setVerticalSpacing(1);
			gridView.setHorizontalSpacing(1);
			List<Object> gridViewAndAdapter=new ArrayList<Object>();
			
			CalendarAdapter calendarAdapter=new CalendarAdapter(context,gridView,calendar,list,lines,isNow);
			
			gridView.setAdapter(calendarAdapter);
			gridViewAndAdapter.add(gridView);
			gridViewAndAdapter.add(calendarAdapter);
			map.put(transformedPosition, gridViewAndAdapter);
		}
		
	}
	
	
	
	
	public interface MyPagerAdapterCallback{
		public void onSetYearAndMonth(String str);
	}
	
	

}
